﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Main_NewsSearch : System.Web.UI.Page
{
    EncriptManager em = new EncriptManager();
    NewsDao nd = new NewsDao();
    DataTable dt = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            if (Session["maintitle"] != null && Session["maintitle"].ToString() != "")
            {
                TextBox txttitle = new TextBox();
                txttitle.Text = Session["maintitle"].ToString();
                bool flag = CheckSpecialWord(txttitle);
                if (flag)
                {
                    string script = "<script>window.alert('对不起，无此相关信息！');location.href='../index.aspx';</script>";
                    Page.RegisterClientScriptBlock("", script);
                }
                ViewState["pageindex"] = "0";
                lbpre.Visible = false;
                lbnext.Visible = false;
                BindData();
            }
            else
            {
                string script = "<script>window.alert('对不起，无此相关信息！');location.href='../index.aspx';</script>";
                Page.RegisterClientScriptBlock("", script);
            }
        }
    }

    protected void IndexChanging(object sender, EventArgs e)
    {
        string strCommand = ((LinkButton)sender).CommandArgument.ToString();
        int pageindex = int.Parse(ViewState["pageindex"].ToString());
        if (strCommand == "pre")
        {
            pageindex = pageindex - 1;
        }
        else
        {
            pageindex = pageindex + 1;
        }
        ViewState["pageindex"] = pageindex;
        BindData();
    }

    private void BindData()
    {
        dt = nd.getNews("0","0",Session["maintitle"].ToString());
        if (dt != null && dt.Rows.Count > 0)
        {
            DataView objView = dt.DefaultView;
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = objView;
            objPds.AllowPaging = true;
            objPds.PageSize = 19;
            objPds.CurrentPageIndex = int.Parse(ViewState["pageindex"].ToString());
            if (!objPds.IsFirstPage)
            {
                lbpre.Visible = true;
            }
            else
            {
                lbpre.Visible = false;
            }
            if (!objPds.IsLastPage)
            {
                lbnext.Visible = true;
            }
            else
            {
                lbnext.Visible = false;
            }
            dlnewslist.DataSource = objPds;
            dlnewslist.DataBind();
            for (int i = 0; i < objPds.Count; i++)
            {
                int size = Convert.ToInt32(ViewState["pageindex"]) * 19 + i;
                string id = dt.Rows[size][0].ToString();
                id = em.Encript(id);
                HyperLink hl = (HyperLink)dlnewslist.Items[i].FindControl("hlnews") as HyperLink;
                while (hl.ToolTip.IndexOf("<") >= 0 && hl.ToolTip.IndexOf(">") >= 0)
                {
                    int start = hl.ToolTip.IndexOf("<");
                    int end = hl.ToolTip.IndexOf(">");
                    if (start > end)
                    {
                        hl.ToolTip = hl.ToolTip.Replace(">", "");
                    }
                    else
                    {
                        string rep = hl.ToolTip.Substring(start, end - start + 1);
                        hl.ToolTip = hl.ToolTip.Replace(rep, "");
                    }
                    hl.ToolTip = hl.ToolTip.Replace("&nbsp;", "");
                    hl.ToolTip = hl.ToolTip.Replace("&#8212;", "—");
                    hl.ToolTip = hl.ToolTip.Replace("&#8216;", "‘");
                    hl.ToolTip = hl.ToolTip.Replace("&#8217;", "’");
                    hl.ToolTip = hl.ToolTip.Replace("&#8220;", "“");
                    hl.ToolTip = hl.ToolTip.Replace("&#8221;", "”");
                    hl.ToolTip = hl.ToolTip.Replace("&#8230;", "…");
                }
                if (hl.ToolTip.Length > 200)
                {
                    hl.ToolTip = hl.ToolTip.Substring(0, 200) + "......";
                }
                hl.Text = hl.Text.Replace(Session["maintitle"].ToString(), "<font color='red'>" + Session["maintitle"].ToString() + "</font>");
                hl.NavigateUrl = string.Format("News.aspx?nid={0}", id);
                Label labtime = (Label)dlnewslist.Items[i].FindControl("labtime") as Label;
                DateTime t = Convert.ToDateTime(labtime.Text);
                labtime.Text = t.ToShortDateString();
            }
        }
        else
        {
            string script = "<script>window.alert('对不起，无此相关信息！');location.href='../index.aspx';</script>";
            Page.RegisterClientScriptBlock("", script);
        }
    }

    public bool CheckSpecialWord(TextBox txtbox)
    {
        bool flag = false;
        ArrayList list = new ArrayList();
        list.Add(" ");
        list.Add("~");
        list.Add("!");
        list.Add("@");
        list.Add("#");
        list.Add("$");
        list.Add("%");
        list.Add("^");
        list.Add("&");
        list.Add("*");
        list.Add("(");
        list.Add(")");
        list.Add("_");
        list.Add("+");
        list.Add("<");
        list.Add(">");
        list.Add(",");
        list.Add(".");
        list.Add("/");
        list.Add("\\");
        list.Add("?");
        list.Add(";");
        for (int i = 0; i < txtbox.Text.Length; i++)
        {
            if (list.Contains(txtbox.Text[i].ToString()))
            {
                flag = true;
            }
        }
        return flag;
    }
}
